package com.hw.paltfrom.service.banlist;

import java.io.IOException;
import java.math.BigDecimal;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.controller.system.BaseFrontController;
import com.hw.paltfrom.mapper.HwBlacklistCarDetailsMapper;
import com.hw.paltfrom.mapper.HwBlacklistCarMapper;
import com.hw.paltfrom.pojo.HwBlacklistCar;
import com.hw.paltfrom.pojo.HwBlacklistCarDetails;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.ExcelFormatUtil;
import com.hw.paltfrom.utils.MapRemoveNullUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BlacklistCarDetailsVo;
import com.hw.paltfrom.vo.BlacklistCarVo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

/*
 * 车辆黑名单
 * 邢家华
 * 2019-11-6 16:12:22
 */
@Service
public class BlacklistCarService {

	@Autowired
	private HwBlacklistCarMapper hwBlacklistCarMapper;

	@Autowired
	private HwBlacklistCarDetailsMapper hwBlacklistCarDetailsMapper;

	/**
	 * slf4j 日志 logger
	 *//*
		 * protected final Logger logger =
		 * LoggerFactory.getLogger(this.getClass());
		 */

	// 黑名单查询
	public Map<String, Object> queryAllList(HashMap<String, Object> vo) {

		int pageNum = Integer.parseInt(vo.get("pageNum").toString());
		int pageSize = Integer.parseInt(vo.get("pageSize").toString());
		String cph = null;
		if (vo.get("cph") != null) {
			cph = (String) vo.get("cph");
		}
		// 分页处理
		PageHelper.startPage(pageNum, pageSize);
		// 去除模糊和分页字段
		vo.remove("pageNum");
		vo.remove("pageSize");
		vo.remove("cph");
		// 去除传值为空的字段
		MapRemoveNullUtil.removeNullEntry(vo);
		Wrapper<HwBlacklistCar> wrapper = new EntityWrapper<HwBlacklistCar>();
		wrapper.allEq(vo).orderBy("F_CreateDate", false);
		if (cph != null) {
			wrapper.like("CPH", cph);
		}
		wrapper.orderBy("F_CreateDate");
		List<HwBlacklistCar> hwBlacklistCarList = hwBlacklistCarMapper.selectList(wrapper);
		PageInfo<HwBlacklistCar> info = new PageInfo<HwBlacklistCar>(hwBlacklistCarList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	// 黑名单详情查询

	public Map<String, Object> queryDetailsList(BlacklistCarVo vo) {
		int pageNum = vo.getPageNum();
		int pageSize = vo.getPageSize();
		// 分页处理
		PageHelper.startPage(pageNum, pageSize);
		String fBlacklistCar = vo.getfId();

		Wrapper<HwBlacklistCarDetails> wrapper = new EntityWrapper<HwBlacklistCarDetails>();
		wrapper.eq("F_Blacklist_Car", fBlacklistCar).orderBy("CreateTime", false);

		List<HwBlacklistCarDetails> hwBlacklistCarDetailsList = hwBlacklistCarDetailsMapper.selectList(wrapper);
		PageInfo<HwBlacklistCarDetails> info = new PageInfo<HwBlacklistCarDetails>(hwBlacklistCarDetailsList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	// 新增
	public Map<String, Object> insert(BlacklistCarVo vo) {

		HwBlacklistCar hwBlacklistCar = new HwBlacklistCar();

		hwBlacklistCar.setfId(RandomNumberUtil.getNumber());
		hwBlacklistCar.setCph(vo.getCph());
		hwBlacklistCar.setZs(vo.getZs());
		hwBlacklistCar.setXz(vo.getZs());
		hwBlacklistCar.setCx(vo.getCx());
		hwBlacklistCar.setItype(vo.getItype());
		hwBlacklistCar.setCreatetime(new Timestamp(vo.getCreatetime()));
		hwBlacklistCar.setTransferstate(vo.getTransferstate());
		hwBlacklistCar.setTransferfile(vo.getTransferfile());
		hwBlacklistCar.setTransferdatetime(new Timestamp(vo.getTransferdatetime()));
		hwBlacklistCar.setTransferuserid(vo.getTransferuserid());
		hwBlacklistCar.setTransferusername(vo.getTransferusername());
		hwBlacklistCar.setfCreateuserid(vo.getfCreateuserid());
		hwBlacklistCar.setfCreateusername(vo.getfCreateusername());
		hwBlacklistCar.setfCreatedate(new Timestamp(System.currentTimeMillis()));

		Integer insert = hwBlacklistCarMapper.insert(hwBlacklistCar);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);

	}

	// 黑名单修改
	public Map<String, Object> update(BlacklistCarVo vo) {

		HwBlacklistCar hwBlacklistCar = new HwBlacklistCar();
		Wrapper<HwBlacklistCar> wrapper = new EntityWrapper<HwBlacklistCar>();
		wrapper.eq("F_Id", vo.getfId());
		hwBlacklistCar.setTransferstate(vo.getTransferstate());
		hwBlacklistCar.setTransferfile(vo.getTransferfile());
		hwBlacklistCar.setTransferdatetime(new Timestamp(vo.getTransferdatetime()));
		hwBlacklistCar.setTransferuserid(vo.getTransferuserid());
		hwBlacklistCar.setTransferusername(vo.getTransferusername());

		Integer update = hwBlacklistCarMapper.update(hwBlacklistCar, wrapper);
		if (update != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		Wrapper<HwBlacklistCarDetails> hwrapper = new EntityWrapper<HwBlacklistCarDetails>();
		hwrapper.eq("F_Blacklist_Car", vo.getfId());
		List<HwBlacklistCarDetails> list = hwBlacklistCarDetailsMapper.selectList(hwrapper);
		for(int i=0;i<list.size();i++){
			HwBlacklistCarDetails hwBlacklistCarDetails =new HwBlacklistCarDetails();
			hwBlacklistCarDetails.setTransferdatetime(new Timestamp(vo.getTransferdatetime())); 
			Wrapper<HwBlacklistCarDetails> bwrapper = new EntityWrapper<HwBlacklistCarDetails>();
			bwrapper.eq("F_Id", list.get(i).getfId());
			Integer update1 = hwBlacklistCarDetailsMapper.update(hwBlacklistCarDetails,bwrapper);
			if (update1 != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);

	}

	// 新增黑名单详情
	public Map<String, Object> insertDetails(BlacklistCarDetailsVo vo) {
		String fBlacklistCar = vo.getfBlacklistCar();
		String cph = vo.getCph();
		String fOverrunid = vo.getfOverrunid();
		String fOverrunname = vo.getfOverrunname();
		BigDecimal cz = vo.getCz();
		BigDecimal cxs = vo.getCxs();
		Long transferdatetime = vo.getTransferdatetime();
		Long createtime = vo.getCreatetime();
		String fZfid = vo.getfZfid();

		// 查询该车辆详情的车 是否在黑名单中
		HwBlacklistCar car = hwBlacklistCarMapper.selectById(fBlacklistCar);
		if (car == null) {
			return ResponeResultUtil.success(HWSystemCode.CARBLACK_NOT_EXIT);
		}

		HwBlacklistCarDetails hwBlacklistCarDetails = new HwBlacklistCarDetails();
		hwBlacklistCarDetails.setfId(RandomNumberUtil.getNumber());
		hwBlacklistCarDetails.setfBlacklistCar(fBlacklistCar);
		hwBlacklistCarDetails.setCph(cph);
		hwBlacklistCarDetails.setfOverrunid(fOverrunid);
		hwBlacklistCarDetails.setfOverrunname(fOverrunname);
		hwBlacklistCarDetails.setCz(cz);
		hwBlacklistCarDetails.setCxs(cxs);
		hwBlacklistCarDetails.setTransferdatetime(new Timestamp(transferdatetime));
		hwBlacklistCarDetails.setCreatetime(new Timestamp(createtime));
		hwBlacklistCarDetails.setfZfid(fZfid);
		Integer insert = hwBlacklistCarDetailsMapper.insert(hwBlacklistCarDetails);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 导出
	public ResponseEntity<byte[]> exportExcel(BlacklistCarDetailsVo vo) {
		try {
			Wrapper<HwBlacklistCarDetails> wrapper = new EntityWrapper<HwBlacklistCarDetails>();
			wrapper.eq("F_Blacklist_Car", vo.getfId());
			List<HwBlacklistCarDetails> list = hwBlacklistCarDetailsMapper.selectList(wrapper);
			BaseFrontController baseFrontController = new BaseFrontController();
			return baseFrontController.buildResponseEntity(export((List<HwBlacklistCarDetails>) list), "车辆黑名单.xls");
		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;

	}

	private InputStream export(List<HwBlacklistCarDetails> list) {

		ByteArrayOutputStream output = null;
		InputStream inputStream1 = null;
		SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
		SXSSFSheet sheet = wb.createSheet();
		// 设置报表头样式
		CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
		CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

		// 每一列字段名
		String[] strs = new String[] { "静态检测时间","车牌号", "检测站", "静态称重（吨）", "超限吨数", "移交时间"};

		// 字段名所在表格的宽度
		int[] ints = new int[] { 10000, 5000, 5000, 5000, 5000, 10000};

		// 设置表头样式
		ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);

		if (list != null && list.size() > 0) {

			DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentCellValue = "";
			for (int i = 0; i < list.size(); i++) {

				HwBlacklistCarDetails bCarDetails = list.get(i);
				SXSSFRow row = sheet.createRow(i + 1);
				int j = 0;

				SXSSFCell cell = row.createCell(j++);
				Date d = bCarDetails.getjJcsj();
				if(d !=null){
					currentCellValue = formater.format(d);
					cell.setCellValue(currentCellValue); // 静态检测时间
				}else{
					cell.setCellValue("");
				}
				
				cell.setCellStyle(content);
				

				cell = row.createCell(j++);
				cell.setCellValue(bCarDetails.getCph()); // 车牌号
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(bCarDetails.getfOverrunname()); // 检测站
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				double cz=bCarDetails.getCz().doubleValue();
				cell.setCellValue(cz); // 静态称重（吨）
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				double cxs=bCarDetails.getCxs().doubleValue();
				cell.setCellValue(cxs); // 超限吨数
				cell.setCellStyle(content);
				
				cell = row.createCell(j++);
				Date d1 = bCarDetails.getTransferdatetime();
				if(d1!=null){
					currentCellValue = formater.format(d1);
					cell.setCellValue(currentCellValue); // 移交时间
				}else{
					cell.setCellValue("");
				}
				
				cell.setCellStyle(content);
				
				
			}

		}
		try {
			output = new ByteArrayOutputStream();
			wb.write(output);
			inputStream1 = new ByteArrayInputStream(output.toByteArray());
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (output != null) {
					output.close();
					if (inputStream1 != null)
						inputStream1.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return inputStream1;
	}

}
